package visualisation;

import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import main.ThreadCount;
import primitives.cluster.ClusterHead;
import primitives.cluster.IClusterLevel;
import primitives.graph.Node;
import primitives.graph.Transition;
import search.util.SILReader;
import search.util.TreeLoader;
import search.util.TreeLoader2;

/**
 *
 * @author mat
 */
public class ClusterViewerController {

    private ClusterViewer gui;
    private ClusterViewerModel model;
    ThreadCount notifier;

    public ClusterViewer getGui() {
        return gui;
    }

    public void setGui(ClusterViewer gui) {
        this.gui = gui;
    }

    public ClusterViewerModel getModel() {
        return model;
    }

    public void setModel(ClusterViewerModel model) {
        this.model = model;
    }

    public void loadGraph() {
        //dosomething
        ClusterHead ch = null;
        if(model.isClusterHead()){
            
        }
        
        try {
            if(!model.isClusterHead()){
                ch = TreeLoader.loadTreeFromDot(model.getDotName());
            }else{
                ch = TreeLoader2.loadTreeFromSerial(model.getDotName());
            }
            
            primitives.graph.Graph g = new primitives.graph.Graph(ch.getNodes());


            DirectedSparseMultigraph go = new DirectedSparseMultigraph();
            //HashMap<Node,String> nodes = new HashMap<Node,String>();
            for (Node n : g.getNodes()) {
                go.addVertex(n);

            }

            for (Node n : g.getNodes()) {
                for (Transition t : n.getTransitionsAsT()) {
                    go.addEdge(t.getLabel() + "<!--" +  Math.random() + "-->", n, t.getDestinationNode());
                    //JUNG's E set must be distinct, so chuck some random crap on the end of each label
                    //to make each one unique.  HTML is used when rendering so we hide the random crap with a comment
                }
            }
            try {
                if(!model.isClusterHead())
                    ch = SILReader.clusterTreeBySIL(g, new File(model.getSilName()));
                
                HashMap<String, HashSet<Node>> clusters = new HashMap<String, HashSet<Node>>();

                for (IClusterLevel l : ch.getChildren()) {
                    String c = "C" + Math.random();
                    clusters.put(c, new HashSet<Node>());
                    for (Node n : l.getNodes()) {
                        clusters.get(c).add(n);
                    }

                }
                //Logger.getLogger(ClusterViewerController.class.getName()).log(Level.INFO, String.format("KeySet size is %d",clusters.keySet().size()));
                Set<Set<Node>> ass = new HashSet<Set<Node>>();
                for (String key : clusters.keySet()) {
                    ass.add(clusters.get(key));
                }

                //Logger.getLogger(ClusterViewerController.class.getName()).log(Level.WARNING, String.format("Assignments has %d clusters",ass.size()));
                model.setClusterAssignments(ass);
            } catch (FileNotFoundException fne) {
                model.setStatus("SIL file not found.  Unclustered graph will be drawn");
                gui.updateGUI();
                Logger.getLogger(ClusterViewerController.class.getName()).log(Level.WARNING, "404:", fne);
            }

            model.setGraph(go);
            model.setStatus(String.format("Graph loaded.  %d nodes",go.getVertexCount()));
            gui.updateGUI();
            Logger.getLogger(ClusterViewerController.class.getName()).log(Level.INFO, String.format("Sending %s to render", model.getDotName()));
            //gui.drawGraph();
        } catch (Exception e) {
            Logger.getLogger(ClusterViewerController.class.getName()).log(Level.WARNING, "ffffuuuuuu", e);
            model.setStatus("Graph file not found.");
                gui.updateGUI();
        }
    }
}
